$(function() {
    obj.init();
});

var obj = new function () {
    return {
        params: {
            type: 'product',
            goal: null,
            goalId: null,
            target: null,
            manage: 0
        },
        dom: {
            list: null,
            save: null
        },
        init: function () {
            var $this = this;
            this.params.goal   = xhTool.getQuery('goal');
            this.params.goalId = xhTool.getQuery('goalId');
            this.params.target = xhTool.getQuery('target');
            this.params.manage = xhTool.getQuery('manage');

            this.dom.save = window.top.xhTop.getModalConfirmDom();
            this.dom.list = $('#list');
            this.dom.list.datagrid({
                url: baseUrl + 'pm/product/getProducts',
                onLoadSuccess: function (data) {
                    $this.funObj().selectExistPower();
                },
                onSelect: function () {
                    $this.funObj().savePower();
                },
                onUnselect: function () {
                    $this.funObj().savePower();
                },
                onSelectAll: function () {
                    $this.funObj().savePower();
                },
                onUnselectAll: function () {
                    $this.funObj().savePower();
                }
            });

            this.listener();
        },
        listener: function () {
            var $this = this;
            //保存权限
            $this.dom.save.click(function () {
                 $this.funObj().savePower();
            });
            $('#bt_search').click(function () {
                var productName = $('#productName').val();
                $this.funObj().searchProduct(productName);
            });
        },
        funObj: function () {
            var $this = this;
            return {
                selectExistPower: function () {
                    xhExtra.ajaxRequest('Power/getExtraPower', $this.params, {check: true, success: false}, function (data) {
                            //
                            var rows = $('#list').datagrid("getRows");
                            //循环数据找出列表中ID和需要选中数据的ID相等的数据并选中
                            for(var i=0;i<rows.length;i++){
                                var rowId = rows[i].iId;
                                for(var j=0;j<data.powerIds.length;j++){
                                    if(rowId==data.powerIds[j]){
                                        var index = $("#list").datagrid("getRowIndex",rows[i])
                                        $("#list").datagrid("checkRow",index);
                                    }
                                }
                            }
                    });
                },
                savePower: function () {
                    var rows = $this.dom.list.datagrid('getSelections'),
                        ids  = [];
                    for(var i=0; i < rows.length; i++){
                        ids.push(rows[i].iId);
                    }
                    // xhExtra.ajaxRequest('Power/saveExtraPower', $.extend({}, $this.params, {ids: ids}), {check: false, success: true}, function (result) {
                    //     if (xhExtra.checkError(result)) {
                    //         xhExtra.setBtnNormal($this.dom.save);
                    //         return false;
                    //     }
                    //     window.top.xhTop.closeModal();
                    // });
                    xhExtra.ajaxRequest('Power/saveExtraPower', $.extend({}, $this.params, {ids: ids}), {check: true, success: false});
                },
                searchProduct: function (productName) {
                    $this.dom.list.datagrid('reload',{'productName':productName});
                }
            }
        }
    }
};